package com.sale.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sale.common.core.utils.bean.BeanUtils;
import com.sale.system.api.model.MaterialUnitDto;
import com.sale.system.domain.SysAuxiliaryData;
import com.sale.system.domain.SysAuxiliaryUnit;
import com.sale.system.domain.SysAuxiliaryUnitDetail;
import com.sale.system.mapper.SysAuxiliaryDataMapper;
import com.sale.system.mapper.SysAuxiliaryUnitDetailMapper;
import com.sale.system.mapper.SysAuxiliaryUnitMapper;
import com.sale.system.service.ISysAuxiliaryUnitDetailService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: YSL
 * @Date: 2023/7/14 9:47
 * @Desc: 辅助单位明细 业务层
 */
@RequiredArgsConstructor
@Service
public class SysAuxiliaryUnitDetailServiceImpl implements ISysAuxiliaryUnitDetailService {

    @Resource
    SysAuxiliaryDataMapper sysAuxiliaryDataMapper;

    @Resource
    SysAuxiliaryUnitDetailMapper sysAuxiliaryUnitDetailMapper;

    @Resource
    SysAuxiliaryUnitMapper sysAuxiliaryUnitMapper;

    /**
     * 根据物料编码查询出单位信息和辅助单位(返回map)
     */
    @Override
    public Map<String, MaterialUnitDto> getUnitByCodeToMap() {
        Map<String, MaterialUnitDto> map = new HashMap<>();

        //单个单位
        List<SysAuxiliaryData> sysAuxiliaryData = sysAuxiliaryDataMapper.selectList(null);
        for (SysAuxiliaryData entity : sysAuxiliaryData) {
            MaterialUnitDto materialUnitDto = new MaterialUnitDto();
            materialUnitDto.setUnitName(entity.getAuxiliaryName());
            map.put(String.valueOf(entity.getAuxiliaryId()), materialUnitDto);
        }

        //多单位
        List<SysAuxiliaryUnitDetail> sysAuxiliaryUnitDetails = sysAuxiliaryUnitDetailMapper.selectList(null);
        for (SysAuxiliaryUnitDetail entity : sysAuxiliaryUnitDetails) {
            MaterialUnitDto materialUnitDto1 = new MaterialUnitDto();
            materialUnitDto1.setUnitName(entity.getUnitName());
            materialUnitDto1.setUnitQuantity(String.valueOf(entity.getUnitQuantity()));
            map.put(String.valueOf(entity.getAuxiliaryUnitId()), materialUnitDto1);
        }
//
//        List<SysAuxiliaryUnit> moreUnit = sysAuxiliaryUnitMapper.selectList(null);
//        for (SysAuxiliaryUnit entity : moreUnit) {
//            List<SysAuxiliaryUnitDetail> sysAuxiliaryUnitDetails = sysAuxiliaryUnitDetailMapper.selectList(new QueryWrapper<SysAuxiliaryUnitDetail>()
//                .lambda().eq(SysAuxiliaryUnitDetail::getAuxiliaryId, entity.getUnitId()));
//            if (sysAuxiliaryUnitDetails.size() > 0) {
//                MaterialUnitDto materialUnitDto = new MaterialUnitDto();
//                materialUnitDto.setUnitName(entity.getAuxiliaryUnitName());
//                map.put(String.valueOf(entity.getUnitId()), materialUnitDto);
//                for (SysAuxiliaryUnitDetail auxiliaryUnitDetail : sysAuxiliaryUnitDetails) {
//                    MaterialUnitDto materialUnitDto1 = new MaterialUnitDto();
//                    materialUnitDto1.setUnitName(auxiliaryUnitDetail.getUnitName());
//                    materialUnitDto1.setUnitQuantity(String.valueOf(auxiliaryUnitDetail.getUnitQuantity()));
//                    map.put(String.valueOf(auxiliaryUnitDetail.getAuxiliaryUnitId()), materialUnitDto1);
//                }
//            }
//        }
        return map;
    }

    /**
     * 获取多单位列表(单位转换使用)
     */
    @Override
    public List<com.sale.system.api.domain.SysAuxiliaryUnitDetail> getUnitByMaterialId(Long materialId) {
        List<SysAuxiliaryUnitDetail> sysAuxiliaryUnitDetails = sysAuxiliaryUnitDetailMapper.selectList(new QueryWrapper<SysAuxiliaryUnitDetail>()
            .lambda().eq(SysAuxiliaryUnitDetail::getMaterialId, materialId));
        List<com.sale.system.api.domain.SysAuxiliaryUnitDetail> result = new ArrayList<>();
        for (SysAuxiliaryUnitDetail entity : sysAuxiliaryUnitDetails) {
            com.sale.system.api.domain.SysAuxiliaryUnitDetail sysAuxiliaryUnitDetail = new com.sale.system.api.domain.SysAuxiliaryUnitDetail();
            BeanUtils.copyProperties(entity, sysAuxiliaryUnitDetail);
            result.add(sysAuxiliaryUnitDetail);
        }
        return result;
    }
}
